package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.DaoSchoolTopDao;
import com.sc.nft.entity.DaoSchoolTop;
import com.sc.nft.entity.dto.DaoSchoolTopSaveAdminDTO;
import com.sc.nft.entity.dto.DaoSchoolTopUpdateByIdAdminDTO;
import com.sc.nft.entity.vo.DaoSchoolTopHomeListVO;
import com.sc.nft.entity.vo.DaoSchoolTopPageAdminVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.DaoSchoolTopService;
import com.sc.nft.sup.PageRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * dao学堂的一级分类(DaoSchoolTop)表服务实现类
 *
 * @author makejava
 * @since 2023-03-13 16:05:12
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class DaoSchoolTopServiceImpl implements DaoSchoolTopService {

    private final DaoSchoolTopDao daoSchoolTopDao;

    /**
     * 分页获取1级分类列表
     *
     * @param pageRequest
     * @return
     */
    @Override
    public Page<DaoSchoolTopPageAdminVO> pageAdmin(PageRequest pageRequest) {
        return daoSchoolTopDao.pageAdmin(pageRequest);
    }

    /**
     * 获取全部数据
     *
     * @return
     */
    @Override
    public List<DaoSchoolTopPageAdminVO> allAdmin() {
        return daoSchoolTopDao.allAdmin();
    }

    /**
     * 后台新增
     *
     * @param daoSchoolTopSaveAdminDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveAdmin(DaoSchoolTopSaveAdminDTO daoSchoolTopSaveAdminDTO) {
        // 名字不可以相同的.所有的名字必须去掉前后空格
        Boolean selectByNameCountIsExist = daoSchoolTopDao.selectByNameCountIsExist(daoSchoolTopSaveAdminDTO.getName().trim());
        if (selectByNameCountIsExist) {
            throw new GlobalRunTimeException("存在同名数据,请更正后在进行操作");
        }
        DaoSchoolTop daoSchoolTop = new DaoSchoolTop();
        daoSchoolTop.setDaoId(6L);
        BeanUtil.copyProperties(daoSchoolTopSaveAdminDTO, daoSchoolTop);
        daoSchoolTop.setName(daoSchoolTop.getName().trim());
        daoSchoolTop.setArticleTotal(0);
        return daoSchoolTop.insert();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByIdAdmin(DaoSchoolTopUpdateByIdAdminDTO daoSchoolTopUpdateByIdAdminDTO) {
        DaoSchoolTop daoSchoolTop = daoSchoolTopDao.selectById(daoSchoolTopUpdateByIdAdminDTO.getId());
        if (BeanUtil.isEmpty(daoSchoolTop)) {
            throw new GlobalRunTimeException("数据不存在或已被删除");
        }
        // 参数保留一份.判定是否有空参数传入进来
        DaoSchoolTopUpdateByIdAdminDTO daoInfo = new DaoSchoolTopUpdateByIdAdminDTO();

        // 复制一份参数
        BeanUtil.copyProperties(daoSchoolTopUpdateByIdAdminDTO, daoInfo);

        // 必传参数设置为空以后判定对象是否存在
        daoSchoolTopUpdateByIdAdminDTO.setId(null);

        if (BeanUtil.isEmpty(daoSchoolTopUpdateByIdAdminDTO)) {
            throw new GlobalRunTimeException("无修改请求参数");
        }

        if (!StrUtil.isEmpty(daoInfo.getName())) {
            Boolean selectByNameCountIsExistNotId = daoSchoolTopDao.selectByNameCountIsExistNotId(daoInfo.getName(), daoInfo.getId());
            if (selectByNameCountIsExistNotId) {
                throw new GlobalRunTimeException("名称已被占用");
            }
        }
        return daoSchoolTopDao.updateById(daoInfo);
    }

    /**
     * 对应条目的数据+1
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void totalAddOne(Long id) {
        daoSchoolTopDao.totalAddOne(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void totalSubOne(Long id) {
        daoSchoolTopDao.totalSubOne(id);
    }

    @Override
    public void totalAddSum(Long id, Integer sum) {
        daoSchoolTopDao.totalAddSum(id, sum);
    }

    @Override
    public void totalSubSum(Long id, Integer sum) {
        daoSchoolTopDao.totalSubSum(id, sum);
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        DaoSchoolTop daoSchoolTop = daoSchoolTopDao.selectById(id);
        if (BeanUtil.isEmpty(daoSchoolTop)) {
            throw new GlobalRunTimeException("当前数据不存在");
        }
        if (daoSchoolTop.getArticleTotal() > 0) {
            throw new GlobalRunTimeException("不可删除当前分组,当前分组下子集中存在未删除的文章");
        }
        return daoSchoolTopDao.deleteById(id);
    }

    @Override
    public DaoSchoolTop selectById(Long id) {
        return daoSchoolTopDao.selectById(id);
    }

    /**
     * 首页获取父集列表
     *
     * @return
     */
    @Override
    public Page<DaoSchoolTopHomeListVO> getHomeTopList(PageRequest pageRequest) {
        return daoSchoolTopDao.getHomeTopList(pageRequest);
    }

}
